package com.sondon.mayi.fragment;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.MyPrefs_;
import com.sondon.mayi.util.ToastUtils;

import de.greenrobot.event.EventBus;

@EFragment
public class BaseFragment extends Fragment {
	protected static final String TAG = "BaseFragment";
	@Pref
	MyPrefs_ myprefs;
	
	Context context;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//注册EventBus
		EventBus.getDefault().register(this);
		this.context=getActivity();
	}
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		//注销EventBus
		EventBus.getDefault().unregister(this);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		
	}
	
	// eventbus事件
	public void onEventMainThread(Object event) {
		LogUtil.d("eventbus  shop", "BaseFragment ...");

	}
	
	/*************************************************** OpenActivity ******************************************************************/
	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
	}

	protected void openActivity_Login(Class<?> pClass) {
		if(myprefs.is_login().get()){
			openActivity(pClass, null);
		}else {
			ToastUtils.show(getActivity(), "请登录!");
		}
	}

	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(getActivity(), pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
		/**
		 * 切换activity动画
		 * 在startActivity（）或finish（）后，调用overridePendingTransition（R.
		 * anim.**in,R.anim.**out）方法.
		 */
		getActivity().overridePendingTransition(R.anim.push_left_in,
				R.anim.push_left_out);
	}

	protected void openActivity(String pAction) {
		openActivity(pAction, null);
	}

	protected void openActivity(String pAction, Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	protected void openActivityForResult(Class<?> pClass, Bundle pBundle,
			int requestCode) {
		Intent intent = new Intent(getActivity(), pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivityForResult(intent, requestCode);
	}

}
